import Cesium from '../Ces/Cesium'


class ViewPoint {
  static PRECISION_LONLAT = 8
  static PRECISION_HEIGHT = ViewPoint.PRECISION_LONLAT / 2
  static PRECISION_ANGLE = ViewPoint.PRECISION_LONLAT / 3
  _position = new Cesium.Cartographic()
  heading
  pitch
  roll
  constructor(options: any = {}) {
    this._position.longitude = options.longitude?? 0.0
    this._position.latitude = options.latitude?? 0.0
    this._position.height = options.height?? 0.0
    this.heading = options.heading?? 0.0
    this.pitch = options.pitch?? -0.5 * Cesium.Math.PI
    this.roll = options.roll?? 0.0
  }

  static clone(viewpoint, result = null) {
    if (!Cesium.defined(viewpoint)) {
      return undefined
    }
    if (!Cesium.defined(result)) {
      result = new ViewPoint({})
    }
    result._position = Cesium.Cartographic.clone(viewpoint._position, result._position)
    result.heading = viewpoint.heading
    result.pitch = viewpoint.pitch
    result.roll = viewpoint.roll

    return result
  }

  static fromCamera(camera, result) {
    if (!Cesium.defined(result)) {
      result = new ViewPoint()
    }

    Cesium.Cartographic.clone(camera.positionCartographic, result._position)
    result.roll = camera.roll
    result.heading = camera.heading
    result.pitch = camera.pitch

    return result
  }

  fromDegrees(x, y, z, roll, heading, pitch) {
    Cesium.Cartographic.fromDegrees(x, y, z, this._position)
    this.roll = Cesium.Math.toRadians(roll)
    this.heading = Cesium.Math.toRadians(heading)
    this.pitch = Cesium.Math.toRadians(pitch)
  }

  get cartesion3() {
    return Cesium.Cartesian3.fromRadians(this._position.longitude, this._position.latitude, this._position.height)
  }
  set cartesion3(value) {
    const cartographic = Cesium.Cartographic.fromCartesian(value)

    this._position.longitude = Cesium.Math.toDegrees(cartographic.longitude)
    this._position.latitude = Cesium.Math.toDegrees(cartographic.latitude)
    this._position.height = cartographic.height
  }

  get cartographic() {
    return this._position
  }
  set cartographic(value) {
    this._position = value
  }

  get height() {
    return this._position.height
  }
  set height(value) {
    this._position.height = value
  }
  get longitudeD() {
    return Cesium.Math.toDegrees(this._position.longitude)
  }
  set longitudeD(value) {
    this._position.longitude = Cesium.Math.toRadians(value)
  }
  get longitude() {
    return this._position.longitude
  }
  set longitude(value) {
    this._position.longitude = value
  }
  get latitude() {
    return this._position.latitude
  }
  set latitude(value) {
    this._position.latitude = value
  }

  get position() {
    return this._position
  }

  toJSON() {
    const precision = ViewPoint.PRECISION_LONLAT
    const longitude = `${this.longitude.toFixed(precision)}`
    const latitude = `${this.latitude.toFixed(precision)}`
    const height = `${this._position.height.toFixed(3)}`
    const heading = `${this.heading.toFixed(3)}`
    const pitch = `${this.pitch.toFixed(3)}`
    const roll = `${this.roll.toFixed(3)}`
    return JSON.stringify({
      longitude: Number(longitude),
      latitude: Number(latitude),
      height: Number(height),
      pitch: Number(pitch),
      heading: Number(heading),
      roll: Number(roll),
    })
  }
}


export default ViewPoint
